"""
核心代码
"""
from interface import user_interface
from interface import bank_interface
from interface import shop_interface
from lib import common

login_user = None

# 注册功能
def register():
    while True:
        # 1) 让用户输入用户名与密码进行校验
        username = input('请输入您的用户名：').strip()
        password = input('请输入密码：').strip()
        re_password = input('请确认密码：').strip()

        # 2) 校验两次密码输入的是否一致
        if password == re_password:
            # 3) 调用用户接口层
            flag,msg = user_interface.register_interface(
                username,password
            )
            if flag:
                print(msg)
                break

            else:
                print(msg)

        else:
            print('两次密码不一致，请重新输入！')

# 登录功能
def login():
    while True:
        username = input('请输入您的用户名：').strip()
        password = input('请输入密码：').strip()

        # 1) 调用接口层进行身份校验
        flag,msg = user_interface.login_interface(username,password)

        if flag:
            print(msg)
            # 记录用户状态
            global login_user
            login_user = username

            break
        else:
            print(msg)

# 查看余额功能
@common.login_auth
def check_balance():
    balnce = user_interface.check_balance_interface(login_user)
    print(f'用户：[{login_user}] 账户余额[{balnce}]元！')

# 提现功能
@common.login_auth
def withdraw():
    while True:
        money = input('请输入您要体现的金额：').strip()
        if not money.isdigit():
            print('请重新输入！')

        flag,msg = bank_interface.withdraw_interface(login_user,money)

        if flag:
            print(msg)
            break
        else:
            print(msg)

# 还款功能
@common.login_auth
def repay():
    while True:
        # 让用户输入还款金额
        input_money = input('请您输入还款金额：').strip()
        if not input_money.isdigit():
            print('请重新输入!')
            continue
        if int(input_money) > 0:

            flag,msg = bank_interface.repay_interface(login_user,input_money)

            if flag:
                print(msg)
                break
            else:
                print('输入的金额不能小于0')

# 转账功能
@common.login_auth
def transfer():
    while True:
        to_user = input('请输入转账的目标用户：').strip()
        money = input('请输入转账金额：').strip()
        if not money.isdigit():
            print('请重新输入！')
            continue

        flag,msg = bank_interface.transfer_interface(login_user,to_user,money)

        if flag:
            print(msg)
            break
        else:
            print(msg)

# 查看流水功能
@common.login_auth
def check_flow():
    flow_list = bank_interface.check_flow_interface(
        login_user
    )
    if flow_list:
        for flow in flow_list:
            print(flow)

    else:
        print(f'当前用户：[{login_user}]没有流水！！')

# 购物功能
@common.login_auth
def shopping():
    shop_dic = [
        ['红烧狮子头',120],
        ['macbook',12000],
        ['飞机',91200],
        ['红烧肉',80],
        ['碳酸饮料',5],
        ['羊肉串套餐',200],
        ['迷你狙击枪',240],
        ['篮球',300],
    ]

    # 初始当前购物车
    shop_car = {}    # {'商品名称'：['单价','数量']}


    while True:
        print('================欢迎来到购物城=============')
        for index,shop in enumerate(shop_dic):
            shop_name,shop_price = shop
            print(f'商品编号：[{index}]',
            f'商品名称：[{shop_name}]',
            f'商品价格：[{shop_price}]')
        print('===============24小时服务=================')

        choice = input('请您选择您要购买的商品编号(是否结账 y or n)：').strip()

        if choice == 'q':
            break

        if choice == 'y':
            if not shop_car:
                print('购物车是空的，不能支付，请重新输入！')
                continue

            # 调用支付接口
            flag,msg = shop_interface.shopping_interface(
                login_user,shop_car
            )
            if flag:
                print(msg)
                break
            else:
                print(msg)
                continue

        if choice == 'n':
            # 调用用户保存购物车接口
            flag,msg = shop_interface.save_shop_car_interface(
                login_user,shop_car
            )

            if flag:
                print(msg)
                break
            else:
                print(msg)


        if not choice.isdigit():
            print('请输入数字！')
            continue

        choice = int(choice)

        # 判断choice是否存在
        if choice not in range(len(shop_dic)):
            print('请输入正确的编号！')
            continue

        # 获取用户购买的商品的名称和单价
        shop_name,shop_price = shop_dic[choice]

        # 加入购物车
        # 判断商品是否已加入，重复的商品+1
        if shop_name in shop_car:
            shop_car[shop_name][1] += 1

        else:
            shop_car[shop_name] = [shop_price,1]

        print('当前购物车：',shop_car)

# 查看购物车功能
@common.login_auth
def check_shopping_car():
    shop_car = shop_interface.check_shopping_car_interface(
        login_user
    )
    print('用户购物车数据：',shop_car)

# 清空购物车
@common.login_auth
def clean_shop_car():
    flag,msg = shop_interface.clean_shop_car_interface(
        login_user
    )

    if flag:
        print(msg)

    else:
        print(msg)

# 管理员功能
@common.login_auth
def admin():
    from core import admin
    admin.admin_run()



# 函数字典
func_dic = {
    '1': register,
    '2': login,
    '3': check_balance,
    '4': withdraw,
    '5': repay,
    '6': transfer,
    '7': check_flow,
    '8': shopping,
    '9': check_shopping_car,
    '10':clean_shop_car,
    '11': admin,
}

# 启动函数
def run():
    while True:
        print('''
        ======= ATM+购物车 ======
               1、注册功能
               2、登录功能
               3、查看金额
               4、提现功能
               5、还款功能
               6、转账功能
               7、查看流水
               8、购物功能
               9、查看购物车
               10、清空购物车
               11、管理员功能
        ========= end ==========
        ''')
        choice = input('请选择您需要的功能编号(温馨提示您，输入q退出)：').strip()

        if choice == 'q':
            break

        if choice not in func_dic:
            print('请输入正确的编号！')
            continue

        func_dic.get(choice)()


